 package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.domain.People;
import es.ats.codington.festival.exceptions.ServiceException;
import es.ats.codington.festival.services.VisitorService;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    
    /**
     * Invokes the action "login.jsp" and closes the session
     */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
		rd.include(request, response);
		HttpSession session = request.getSession();
		session.invalidate();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	/**
	 * Method invoked when the visitor is logs on
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();  
        
		/**
		 * Opening the session
		 */
		HttpSession session = request.getSession(true);
		
	    String n=request.getParameter("username");  
	    String p=request.getParameter("password");  
	    
	    VisitorService service = new VisitorService();
	    //VisitorService service2 = new VisitorService();
	    
	    Visitor visitor = new Visitor();
	    visitor.setUserName(n);
	    visitor.setPassword(p);
	    
	    /**
	     * Checks whether the visitor is logged
	     */
	    try {
			if(service.checkLogin(visitor)){
				
				/**
				 * If the visitor is admin
				 */
				if(service.checkAdmin(visitor)){
					
					
					session.setAttribute("visitor", visitor);
					session.setAttribute("username", visitor.getUserName());
					session.setAttribute("password", visitor.getPassword());
					/**
					 * Invokes the action "administor.jsp"
					 */
					response.sendRedirect("adminvisitor.jsp");
				
				/**
				 * If visitor in not admin
				 */
				} else {
					
					//session.setAttribute("IDVISITOR", visitor.getVisitorId());
					session.setAttribute("username", visitor.getUserName());
					session.setAttribute("password", visitor.getPassword());
					
					int id = service.getIdVisitor(visitor);
					People people = new People();
					People people2 = new People();
					people.setIdPeople(id);
					people2 = service.searchPeople(people);
					session.setAttribute("firstNamePeople", people2.getFirstName());
					session.setAttribute("lastNamePeople", people2.getLastName());
					session.setAttribute("dniPeople", people2.getDni());
					session.setAttribute("addressPeople", people2.getAddress());
					session.setAttribute("numberPeople", people2.getPhoneNumber());
					session.setAttribute("emailPeople", people2.getEmail());
					
					/**
					 * Invokes the action PersonalServlet
					 */
					response.sendRedirect("PersonalServlet");
				}
			      
			    
			}  else{  
				/**
				 * The visitor is not logged
				 */
				request.setAttribute("error", "Unknown user, please try again");
				
				/**
			     * Invokes the action "login.jsp"
			     */
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	    
	    out.close(); 
	    
	}

}
